class Solution30 {
    public boolean[][] visit;
    public int numIslands(char[][] grid) {
        int count = 0;
        visit = new boolean[grid.length][grid[0].length];
        for(int i = 0; i < grid.length; i++) {
            for(int j = 0; j < grid[0].length; j++) {
                if(grid[i][j] == '1' && visit[i][j] == false) {
                    visit[i][j] = true;
                    dfs(grid,i,j);
                    count++;
                }
            }
        }
        return count;
    }
    int[] dx ={1,-1,0,0};
    int[] dy ={0,0,1,-1};
    public void dfs(char[][] grid,int x,int y) {
        for(int k = 0; k < 4; k++) {
            int x1 = x + dx[k];
            int y1 = y + dy[k];
            if(x1 >= 0 && x1 < grid.length && y1 >=0 && y1 < grid[0].length && grid[x1][y1] == '1' && visit[x1][y1] == false) {
                visit[x1][y1] = true;
                dfs(grid,x1,y1);
            }
        }
        
    }
}